package  
{
	import org.flixel.*;
	
	/**
	 * ...
	 * @author ...
	 */
	public class Level4State extends PlayState 
	{
		[Embed(source = "../data/levels/Level 4.oel", mimeType = "application/octet-stream")] private var _newLevel:Class;
		
		private var _carriages:MovingObjectCirc;
		
		protected var _cloudLow1:FlxSprite;
		protected var _cloudLow2:FlxSprite;
		protected var _cloudLow3:FlxSprite;
		protected var _cloudMid1:FlxSprite;
		protected var _cloudMid2:FlxSprite;
		protected var _cloudMid3:FlxSprite;
		protected var _cloudHigh1:FlxSprite;
		protected var _cloudHigh2:FlxSprite;
		protected var _cloudHigh3:FlxSprite;
		protected var _fairSkyline1:FlxSprite;
		protected var _fairSkyline2:FlxSprite;
		protected var _fairSkyline3:FlxSprite;
		protected var _fairLights:FlxSprite;
		
		override public function create():void
		{
			_background = new FlxSprite(0, 0).createGraphic(FlxG.width, FlxG.height, 0xff000000);
			_background.scrollFactor.x = _background.scrollFactor.y = 0;
			add(_background);
			
			createFairBackground();
			
			super.create();
			
			_playerStartingX = 0;
			_playerStartingY = 420;
			_levelTime = 80;
			
			tilemap.collideIndex = lightningmap.collideIndex = 5;
			
			var roomXML:XML = new XML(new _newLevel);
			
			tilemap.loadOgmoTileMap(roomXML, "tiles", TilesetImages.fair);
			lightningmap.loadOgmoTileMap(roomXML, "tiles", TilesetImages.warehouse);
			objectmap.loadOgmoObjects(roomXML, "objects", "lights", LightBulb, lightBulbs);
			objectmap.loadOgmoObjects(roomXML, "objects", "batteries", Battery, powerups);
			objectmap.loadOgmoObjects(roomXML, "objects", "clocks", Clock, powerups);
			movermap.loadOgmoMovingObjects(roomXML, "objects", "movingPlatform", ImageFiles.elevator, moverGroup);
			
			_carriages = new MovingObjectCirc (ImageFiles.elevator, 60, 5);
			_carriages.setStartPoint(Math.PI, 908, 400);
			_carriages.setVelocity(1);
			_carriages.setCirclingBox(125);
			moverGroup.add(_carriages);
			
			_carriages = new MovingObjectCirc (ImageFiles.elevator, 60, 5);
			_carriages.setStartPoint(Math.PI*1/2, 1034, 280);
			_carriages.setVelocity(1);
			_carriages.setCirclingBox(125);
			moverGroup.add(_carriages);
			
			_carriages = new MovingObjectCirc (ImageFiles.elevator, 60, 5);
			_carriages.setStartPoint(Math.PI*3/2, 794, 270);
			_carriages.setVelocity(1);
			_carriages.setCirclingBox(125);
			moverGroup.add(_carriages);
			
			_carriages = new MovingObjectCirc (ImageFiles.elevator, 60, 5);
			_carriages.setStartPoint(Math.PI*2, 908, 150);
			_carriages.setVelocity(1);
			_carriages.setCirclingBox(125);
			moverGroup.add(_carriages);
			
			_levelTransition.init();
			
			FlxG.followBounds(tilemap.left, tilemap.top, tilemap.right, tilemap.bottom);
			
			restartLevel();
			
			FlxG.playMusic(SoundFiles.fairMusic, 0.75);
			FlxG.music.fadeIn(1.0);
		}
		
		protected function createFairBackground():void
		{
			_cloudHigh1 = new FlxSprite(0, FlxG.height, ImageFiles.fairCloudHigh);
			_cloudHigh1.y -= _cloudHigh1.height;
			_cloudHigh1.velocity.x = -5;
			_cloudHigh1.scrollFactor.x = .15;
			_cloudHigh2 = new FlxSprite(FlxG.width, FlxG.height, ImageFiles.fairCloudHigh)
			_cloudHigh2.y -= _cloudHigh2.height;
			_cloudHigh2.velocity.x = -5;
			_cloudHigh2.scrollFactor.x = .15;
			_cloudHigh3 = new FlxSprite(FlxG.width * 2, FlxG.height, ImageFiles.fairCloudHigh)
			_cloudHigh3.y -= _cloudHigh3.height;
			_cloudHigh3.velocity.x = -5;
			_cloudHigh3.scrollFactor.x = .15;
			
			_cloudMid1 = new FlxSprite(0, FlxG.height, ImageFiles.fairCloudMid);
			_cloudMid1.y -= _cloudMid1.height;
			_cloudMid1.velocity.x = -6;
			_cloudMid1.scrollFactor.x = .22;
			_cloudMid2 = new FlxSprite(FlxG.width, FlxG.height, ImageFiles.fairCloudMid)
			_cloudMid2.y -= _cloudMid2.height;
			_cloudMid2.velocity.x = -6;
			_cloudMid2.scrollFactor.x = .22;
			_cloudMid3 = new FlxSprite(FlxG.width * 2, FlxG.height, ImageFiles.fairCloudMid)
			_cloudMid3.y -= _cloudMid3.height;
			_cloudMid3.velocity.x = -6;
			_cloudMid3.scrollFactor.x = .22;
			
			_cloudLow1 = new FlxSprite(0, FlxG.height, ImageFiles.fairCloudLow);
			_cloudLow1.y -= _cloudLow1.height;
			_cloudLow1.velocity.x = -7;
			_cloudLow1.scrollFactor.x = .3;
			_cloudLow2 = new FlxSprite(FlxG.width, FlxG.height, ImageFiles.fairCloudLow)
			_cloudLow2.y -= _cloudLow2.height;
			_cloudLow2.velocity.x = -7;
			_cloudLow2.scrollFactor.x = .3;
			_cloudLow3 = new FlxSprite(FlxG.width * 2, FlxG.height, ImageFiles.fairCloudLow)
			_cloudLow3.y -= _cloudLow3.height;
			_cloudLow3.velocity.x = -7;
			_cloudLow3.scrollFactor.x = .3;
			
			var fairBackground:FlxSprite = new FlxSprite(0, FlxG.height, ImageFiles.fairBackground);
			fairBackground.y -= fairBackground.height;
			fairBackground.scrollFactor.x = 0;
			
			_fairLights = new FlxSprite(0, 0, ImageFiles.fairLights);
			_fairLights.scrollFactor.x = _fairLights.scrollFactor.y = 0;
			_fairLights.alpha = 0;
			
			_fairSkyline1 = new FlxSprite(0, FlxG.height, ImageFiles.fairSkyline);
			_fairSkyline1.y -= _fairSkyline1.height;
			_fairSkyline1.scrollFactor.x = .37;
			_fairSkyline2 = new FlxSprite(FlxG.width, FlxG.height, ImageFiles.fairSkyline)
			_fairSkyline2.y -= _fairSkyline2.height;
			_fairSkyline2.scrollFactor.x = .37;
			_fairSkyline3 = new FlxSprite(FlxG.width * 2, FlxG.height, ImageFiles.fairSkyline)
			_fairSkyline3.y -= _fairSkyline3.height;
			_fairSkyline3.scrollFactor.x = .37;
			
			add(_cloudHigh1);
			add(_cloudHigh2);
			add(_cloudHigh3);
			add(_cloudMid1);
			add(_cloudMid2);
			add(_cloudMid3);
			add(_cloudLow1);
			add(_cloudLow2);
			add(_cloudLow3);
			add(fairBackground);
			add(_fairLights);
			add(_fairSkyline1);
			add(_fairSkyline2);
			add(_fairSkyline3);
		}
		
		override public function update():void 
		{
			updateFairBackground();
			
			super.update();
		}
		
		protected function updateFairBackground():void
		{
			if (_cloudLow1.x <= -FlxG.width)
			{
				_cloudLow1.x = FlxG.width * 2;
			}
			if (_cloudLow2.x <= -FlxG.width)
			{
				_cloudLow2.x = FlxG.width * 2;
			}
			if (_cloudLow3.x <= -FlxG.width)
			{
				_cloudLow3.x = FlxG.width * 2;
			}
			if (_cloudMid1.x <= -FlxG.width)
			{
				_cloudMid1.x = FlxG.width * 2;
			}
			if (_cloudMid2.x <= -FlxG.width)
			{
				_cloudMid2.x = FlxG.width * 2;
			}
			if (_cloudMid3.x <= -FlxG.width)
			{
				_cloudMid3.x = FlxG.width * 2;
			}
			if (_cloudHigh1.x <= -FlxG.width)
			{
				_cloudHigh1.x = FlxG.width * 2;
			}
			if (_cloudHigh2.x <= -FlxG.width)
			{
				_cloudHigh2.x = FlxG.width * 2;
			}
			if (_cloudHigh3.x <= -FlxG.width)
			{
				_cloudHigh3.x = FlxG.width * 2;
			}
			
			_fairLights.alpha = Math.abs(1 - desaturation);
		}
	}

}